package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.Student;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by j_hu on 5/12/17.
 */

public class SyncStudentHandler extends SyncMetaCommandRespHandler {
    public SyncStudentHandler(SyncMetaCommand cmd) {
        super("student.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            Gson gson = new Gson();
            SyncMetaCommandRS<SyncStudentListRS> syncRS = (SyncMetaCommandRS<SyncStudentListRS>)gson.fromJson(
                    getString(input),
                    new TypeToken<SyncMetaCommandRS<SyncStudentListRS>>(){}.getType());

            if (syncRS != null && syncRS.getData() != null) {
                getCommand().notify(NotifyMessage.info(
                        String.format("%s<%s>", Consts.INFO_SYNC_STUDENT, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                ArrayList<Student> list = new ArrayList<>();
                for(StudentRS s:syncRS.getData().getList()) {
                    Student student = new Student();
                    student.setNo(s.getNo());
                    student.setClassNo(s.getClassNo());
                    student.setName(s.getName());
                    student.setBirthday(s.getBirthday());
                    student.setImgURL(s.getImgURL());
                    student.setDeleted(s.isDeleted());
                    list.add(student);
                }
                if (getCommand().getDbHelper().groupAddStudents(list)) {
                    getCommand().getDbHelper().getSyncRecord().update(getName(), syncRS.getLastMetaUpdateTime());
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_STUDENT_OK, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                    handleOk = true;
                } else {
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_STUDENT_FAIL, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                }
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }

    public class SyncStudentListRS {
        private List<StudentRS> list;
        public List<StudentRS> getList() {
            return list;
        }
    }

    public class StudentRS {
        private int no;
        private String name;
        private String birthday;
        private boolean deleted;

        @SerializedName("class_no")
        private int classNo;

        @SerializedName("img_url")
        private String imgURL;

        public int getNo() {
            return no;
        }

        public int getClassNo() { return classNo; }

        public String getName() {
            return name;
        }

        public String getBirthday() { return birthday; }

        public String getImgURL() { return imgURL; }

        public boolean isDeleted() { return deleted; }
    }
}
